-
Notifications
You must be signed in to change notification settings - Fork 9
## Optimize validateParentPath for Faster Parent Path Validation
#1396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This commit refactors the `validateParentPath` function in `transaction-shim.ts` to improve its performance, especially for deep or frequent path validations during storage transactions. ### Optimization Details - **Single-pass Traversal:** The previous implementation redundantly checked object-ness and existence at each path level, sometimes re-traversing the same objects. The new approach streamlines the traversal by iterating through the parent path only once, directly walking down the value tree and stopping at the first missing or non-object parent. - **Efficient Error Handling:** Error construction is now more precise, and the function avoids unnecessary object checks and array slicing. The logic for setting the error's `.path` property is simplified, reducing overhead in error cases. - **No Change in Behavior:** All existing functionality and error semantics are preserved. The function still returns the same errors for invalid paths, but does so with less computational overhead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cubic found 2 issues across 1 file. Review them in cubic.dev
React with 👍 or 👎 to teach cubic. Tag @cubic-dev-ai to give specific feedback.
| nextValue = nextValue[key] = typeof Number(key) === "number" ? [] : {}; | ||
| nextValue = | ||
| nextValue[key] = | ||
| (typeof Number(key) === "number" ? [] : {}) as typeof nextValue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typeof Number(key) === "number" is always true, so the code always creates an array and never an object, corrupting the structure when key is non-numeric
| (typeof Number(key) === "number" ? [] : {}) as typeof nextValue; | |
| (Number.isInteger(Number(key)) ? [] : {}) as typeof nextValue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it can't be NaN
| pathError.path.push(segment); | ||
| } | ||
| } | ||
| if (parentIndex > 0) pathError.path = path.slice(0, parentIndex - 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Off-by-one in slice: omits the last valid segment, so pathError.path reports an empty or incomplete parent path
| if (parentIndex > 0) pathError.path = path.slice(0, parentIndex - 1); | |
| if (parentIndex > 0) pathError.path = path.slice(0, parentIndex); |
This commit refactors the
validateParentPathfunction intransaction-shim.tsto improve its performance, especially for deep or frequent path validations during storage transactions.Optimization Details
Single-pass Traversal: The previous implementation redundantly checked object-ness and existence at each path level, sometimes re-traversing the same objects. The new approach streamlines the traversal by iterating through the parent path only once, directly walking down the value tree and stopping at the first missing or non-object parent.
Efficient Error Handling: Error construction is now more precise, and the function avoids unnecessary object checks and array slicing. The logic for setting the error's
.pathproperty is simplified, reducing overhead in error cases.No Change in Behavior: All existing functionality and error semantics are preserved. The function still returns the same errors for invalid paths, but does so with less computational overhead.
Summary by cubic
Refactored the validateParentPath function to validate parent paths in a single pass, improving performance for deep or frequent storage transactions.